package com.qyb.util;

import com.google.zxing.*;
import com.google.zxing.common.HybridBinarizer;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;

/**
 * <p>
 * 实现功能：
 * <p/>
 *
 * @author 秦有宝
 * @version 1.0.00
 */
public class BarcodePdfUtils {
    /**
     * 生成二维码图片并插入pdf中
     * @throws IOException
     * @throws DocumentException
     */
    @Test
    public void CreateBarcodePdf() throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/test/scanner.pdf"));
        //pdf输出地址
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        BarcodeQRCode barcodeQRCode = new BarcodeQRCode("1234567890 ASDF", 1000, 1000, null);
        Image codeQrImage = barcodeQRCode.getImage();
        codeQrImage.scaleAbsolute(100, 100);
        document.add(codeQrImage);
        document.close();
    }

    /**
     * 抽取pdf中二维码图片
     *
     * @throws DocumentException
     * @throws IOException
     */
    @Test
    public void ExtractImages() throws IOException, DocumentException {
        String sourcePDF = "D:/test/scanner.pdf";    //pdf文件
        String destination = "D:/test";               //图片输出地址
        BarcodePdfUtils.extractImages(sourcePDF, destination);
    }

    /**
     * Parses a PDF and extracts all the images.
     *
     * @param filename the source PDF
     * @param destination the directory to save images
     */
    public static void extractImages(String filename, String destination) throws IOException, DocumentException {
        System.out.println("Processing PDF at " + filename);
        System.out.println("Saving images to " + destination);

        PdfReader reader = new PdfReader(filename);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        ImageRenderListener listener = new ImageRenderListener(destination + "/Img");
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            parser.processContent(i, listener);
        }
        reader.close();
    }

    @Test
    public void ParseImage () throws NotFoundException, IOException {
        String decode = decode("d:/tt/pdfbox_image.png");//识别图片路径
        System.out.println(decode); //输出到控制台
    }
    public static String decode(String filepath) throws IOException, NotFoundException {
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(binarizer);
        HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
        decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(bitmap, decodeHints);
        return result.getText();
    }

}
